﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Controls;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using ReflectionStudio.Core.Database;
using ReflectionStudio.Core.FTPClient;
using INTEX.DataBase;
using INTEX.DataBase.Tools;
using INTEX.DataBase;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for JLTVKWZ.xaml
    /// </summary>
    public partial class JLTVKWZ : DocumentBase
    {
        #region-------------Properties-----------------------
        string Mandant = "00";
        ApplicationBase ApplicationBase;
        bool IsSetUp = false;
        INTEXDBC INTEXDBC;
        string PrintType = "";
        public OracleConnection OracleDB;
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        JLTVKWZData m_Data, m_DataX;
        DataRow dwTemp;
        BackgroundWorker bgWorker;
        string lang
        {
            get 
            {
                return txtLang.Text.Trim();
            }
        }
        public SETINTEXDB SetData
        {
            get
            {
                return (SETINTEXDB)this.TryFindResource("SetData");
            }
        }
        public DataRow dwVKPOS;
        public DataRow dwVVLIEFSCH;
        public DataRow dwCOLOR;
        public DataRow dwWBBST;
        public DataSet dsParamaters;
        JLTVKWZReport m_rpt;
        ArrayList listVV06, ListColor, listBatchNo;
        string KRS
        {
            get
            {
                return Helper.Left(txtVKPAUFKRS.Text.ToString() + "  ", 2);
            }
        }
        string LFD
        {
            get
            {
                return Helper.Right("000000" + txtVKPAUFLFD.Text.ToString(), 6);
            }
        }
        #endregion
        public JLTVKWZ()
        {
            InitializeComponent();
            this.Name = "布卷清单";
        }
        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            if (!IsSetUp)
            {
                ApplicationBase.Quit();
            }
            base.OnClosing(e); 
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            try
            {
                IsSetUp = true;
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    base.SetUp(dicParamater);
                    InitializeComponent();
                    this.dicParamater = dicParamater;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!IsSetUp)
                {
                    ApplicationBase = new ApplicationBase();
                    ApplicationBase.Load();
                    dicParamater = ApplicationBase.dicParamater;

                }
                OracleDB = (OracleConnection)dicParamater["OracleDB"];
                proxy = (IntexReptServBLClient)dicParamater["proxy"];
                Mandant = (string)dicParamater["MANDANT"];

                m_Data = new JLTVKWZData(OracleDB, proxy);
                INTEXDBC = new INTEX.DataBase.Tools.INTEXDBC(OracleDB, proxy);
                #region============TESTINGDATA================
                txtVKPAUFKRS.Text = "JL";
                txtVKPAUFLFD.Text = "000001";
                #endregion
                bgWorker = new BackgroundWorker();
                bgWorker.DoWork += bgWorker_DoWork;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            } 
        }

        void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
           
        }
        private void GenerateReport(string PrintType)
        {
            try
            {
                JLTVKWZReport m_rpt = new JLTVKWZReport(dicParamater);
                JLTVKWZData m_DX = new JLTVKWZData(dicParamater);
                m_rpt.dsSource = SetData;
                m_rpt.PrintType = PrintType;
                m_rpt.exAgent = m_DX.exAgent;
                m_rpt.OracleDB = this.OracleDB;
                m_rpt.proxy = this.proxy;
                m_rpt.WriteExcelReport(PrintType, lang);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnPreview_Click(object sender, RoutedEventArgs e)
        {
            PrintType = "OpenExcel";
            GenerateReport(PrintType);
        }
        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            PrintType = "EXCEL";
            GenerateReport(PrintType);
        }
        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    SetData.Clear();
                    SetData.AcceptChanges();
                    dwTemp = SetData.VKPOS.NewRow();
                    dwTemp["VKPMANDANT"] = Mandant;
                    dwTemp["VKPAUFKRS"] = KRS;
                    dwTemp["VKPAUFLFD"] = LFD;
                    SetData.Merge(m_Data.BuildData(dwTemp));
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}
